package cn.xqj.pet.service.impl;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.util.*;
import cn.xqj.order.domain.OrderPetAcquisition;
import cn.xqj.order.mapper.OrderPetAcquisitionMapper;
import cn.xqj.org.domain.Employee;
import cn.xqj.org.domain.Shop;
import cn.xqj.org.mapper.EmployeeMapper;
import cn.xqj.org.mapper.ShopMapper;
import cn.xqj.pet.domain.Pet;
import cn.xqj.pet.domain.SearchMasterMsg;
import cn.xqj.pet.domain.SearchMasterMsgAuditLog;
import cn.xqj.pet.dto.msgDto;
import cn.xqj.pet.mapper.PetMapper;
import cn.xqj.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.xqj.pet.mapper.SearchMasterMsgMapper;
import cn.xqj.pet.query.SearchMasterMsgQuery;
import cn.xqj.pet.service.ISearchMasterMsgService;
import cn.xqj.basic.service.impl.BaseServiceImpl;
import cn.xqj.user.domain.Logininfo;
import cn.xqj.user.domain.User;
import cn.xqj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Console;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    PetMapper petMapper;


    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        Logininfo logininfo = getLogininfo.getLogininfo(request);
        System.out.println(logininfo);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        System.out.println(user);
        searchMasterMsgMapper.add(searchMasterMsg);



        Boolean textCensor = BaiduAuditUtils.TextCensor(searchMasterMsg.getTitle() + searchMasterMsg.getName() + searchMasterMsg.getAddress());
        Boolean imgCensors = BaiduAuditUtils.ImgCensors(searchMasterMsg.getResources());

        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.findAll());

        searchMasterMsg.setUserId(user.getId());

        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        log.setMsgId(searchMasterMsg.getId());

        if (textCensor && imgCensors) {
            searchMasterMsg.setState(1);
            searchMasterMsg.setNote("审核成功！");
            if (nearestShop != null) {
                searchMasterMsg.setShopId(nearestShop.getId());
            }
            searchMasterMsgMapper.update(searchMasterMsg);
            log.setNote("审核成功！");
            log.setState(1);
            searchMasterMsgAuditLogMapper.add(log);
        } else {
            searchMasterMsg.setState(0);
            searchMasterMsg.setNote("审核失败，非法字符！");
            searchMasterMsgMapper.update(searchMasterMsg);
            log.setNote("审核失败，非法字符！");
            log.setState(0);
            searchMasterMsgAuditLogMapper.add(log);
            throw new BusinessException("审核失败，非法字符！");
        }

    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        Logininfo logininfo = getLogininfo.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }


    @Override
    public PageList<SearchMasterMsg> userSearchMasterMsg(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = getLogininfo.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return super.pageQuery(query);
    }

    @Override
    public void reject(Long msgId) {
        searchMasterMsgMapper.reject(msgId);
    }

    @Override
    public void accept(msgDto msgDto) {
        Long empId = msgDto.getHandler();
        Long msgId = msgDto.getMsgId();
        String note = msgDto.getNote();

        OrderPetAcquisition orderTem = orderPetAcquisitionMapper.findByMsgId(msgId);

        if (orderTem != null) {
            throw new BusinessException("订单正在处理，请耐心等待!");
        }

        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findOne(msgId);
        Pet pet = searchMasterMsg2pet(searchMasterMsg);
        petMapper.add(pet);

        // 创建收购单
        OrderPetAcquisition order = buildOrder(pet,searchMasterMsg,empId);
        orderPetAcquisitionMapper.add(order);

        System.out.println("您有新的订单"+ order.getOrderSn() + "请在规定时间完成收购！");
    }
    private OrderPetAcquisition buildOrder(Pet pet, SearchMasterMsg msg, Long empId) {
        OrderPetAcquisition order = new OrderPetAcquisition();
        String orderSn = CodeGenerateUtils.generateOrderSn(pet.getUserId());
        order.setOrderSn(orderSn);
        order.setDigest(msg.getTitle());
        //两天之后的此刻就是最后收购时间 - 自动通过定时器取消
        long timeTem = System.currentTimeMillis() + 2*24*60*60*1000;
        order.setLastcomfirmtime(new Date(timeTem));
        order.setState(0);//待处理
        order.setPrice(new BigDecimal(msg.getPrice()));
        //paytype//在处理订单时才处理
        order.setPetId(pet.getId());
        order.setUserId(pet.getUserId());
        order.setShopId(pet.getShopId());
        order.setAddress(msg.getAddress());
        order.setEmpId(empId);
        order.setSearchMasterMsgId(msg.getId());
        return order;
    }

    private Pet searchMasterMsg2pet(SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(msg.getPrice().toString());
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
